package com.atswust.shake.controller;

import com.atswust.shake.common.Constants;
import com.atswust.shake.config.ResponBean;
import com.atswust.shake.dto.SenderAndMessage;
import com.atswust.shake.pojo.Student;
import com.atswust.shake.service.MessageService;
import com.atswust.shake.service.StudentService;
import com.atswust.shake.utils.JWTutils;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import java.util.List;


/**
 * @author liushuai
 * @create 2022-02-07 11:40
 */
@RestController
@CrossOrigin
@Api(tags="打招呼接口")
@RequestMapping("v2/shake/greeting/")
public class GreetingController {
     @Autowired
    MessageService messageService;
    @Autowired
    StudentService studentService;
    @GetMapping("/senders/list")
    @ApiOperation("返回所有发送者")
    public ResponBean getSenderUser(HttpServletRequest request){
        final String account = getAccount(request);
        Integer id = studentService.getStuByAccount(account).getId();
        List<Student> sendAccountStu = messageService.getSendAccountStu(id);
        return ResponBean.success("查询成功",sendAccountStu);
    }
    @PostMapping("/{receiverId}")
    @ApiOperation("发送消息")
    @ApiImplicitParams({@ApiImplicitParam(name = "receiverId",value = "接收者Id"),
                        @ApiImplicitParam(name="message",value = "发送的信息")}
    )
    public ResponBean send(@PathVariable("receiverId") @Positive(message = "必须为整数") Integer receiveId,
                           @RequestParam("message") @NotBlank(message = "信息不能为空") @Size(max = 100,message = "长度不能超过100") String message,
                           HttpServletRequest request){
        final String account = getAccount(request);
        messageService.sendMessage(studentService.getStuByAccount(account).getId(),message,receiveId);
        return ResponBean.success("发送成功");
    }
    @GetMapping("/senderAndMessage")
    @ApiOperation("返回发送者和其所发的消息")
    public ResponBean getSenderAndMessage(HttpServletRequest request){
        final String account = getAccount(request);
        Integer id = studentService.getStuByAccount(account).getId();
        List<SenderAndMessage> senderAndMessage = messageService.getSenderAndMessage(id);
        return  ResponBean.success("获取成功",senderAndMessage);
    }
    private String getAccount(HttpServletRequest request){
        final String account = JWTutils.getAccountByToken(request.getHeader(Constants.TOKEN));
        return account;
    }
}
